﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Ledict.Core.Data;
using System.Data.Common;
using Ledict.ConditionExtract.Model;
using Ledict.ConditionExtract.Common;
namespace Ledict.ConditionExtract.DAL
{
   public   class CaseCauseInferConditionDAL
    {
       private  string DbName;
       public CaseCauseInferConditionDAL()
       {
           DbName = "JudgeDoc";
       }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(SYSCaseCauseInferCondition Model)
        {
           
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SYS_CaseCause_InferCondition(");
            strSql.Append("IR_ID,CC_ID,IR_Name,IR_Content,IR_Remark,IR_Code)");
            strSql.Append(" values (");
            strSql.Append("@IR_ID,@CC_ID,@IR_Name,@IR_Content,@IR_Remark,@IR_Code)");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@IR_ID", DbType.String, Model.IR_ID);
            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, Model.CC_ID);
            oBase.AddInParameter(oCmd, "@IR_Name", DbType.String, Model.IR_Name);
          
            oBase.AddInParameter(oCmd, "@IR_Content", DbType.String, Model.IR_Content);
            oBase.AddInParameter(oCmd, "@IR_Remark", DbType.String, Model.IR_Remark);
            oBase.AddInParameter(oCmd, "@IR_Code", DbType.String, Model.IR_Code);

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }




        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(SYSCaseCauseInferCondition Model)
        {
            if (Model.IR_Code == "")
            {
                return true;
            }
            else
            {
                Database oBase = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = oBase.GetSqlStringCommand(@"select count(1) from SYS_CaseCause_InferCondition 
                where IR_Code =@IR_Code ");

                oBase.AddInParameter(oCmd, "@IR_Code", DbType.String, Model.IR_Code);


                if (Convert.ToInt32(oBase.ExecuteScalar(oCmd)) > 0)
                {
                    return true;
                }
            }
            return false;
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SYSCaseCauseInferCondition Model)
        {
           
           
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SYS_CaseCause_InferCondition set ");
            strSql.Append("CC_ID=@CC_ID,");
            strSql.Append("IR_Name=@IR_Name,");
           
            strSql.Append("IR_Content=@IR_Content,");
            strSql.Append("IR_Remark=@IR_Remark");
            strSql.Append(" where IR_ID=@IR_ID ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString ());

            oBase.AddInParameter(oCmd, "@IR_ID", DbType.String, Model.IR_ID);
            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, Model.CC_ID);
            oBase.AddInParameter(oCmd, "@IR_Name", DbType.String, Model.IR_Name);
           
            oBase.AddInParameter(oCmd, "@IR_Content", DbType.String, Model.IR_Content);
            oBase.AddInParameter(oCmd, "@IR_Remark", DbType.String, Model.IR_Remark);

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string IR_ID)
        {

           
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SYS_CaseCause_InferCondition ");
            strSql.Append(" where IR_ID=@IR_ID ");

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@IR_ID", DbType.String, IR_ID);

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;

        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataTable GetList(string CC_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM SYS_CaseCause_InferCondition ");

            strSql.Append(" where CC_ID=@CC_ID");
         
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString ());
            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, CC_ID);

            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }



        }

       
        public DataTable GetOneData(string IR_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM SYS_CaseCause_InferCondition ");

            strSql.Append(" where IR_ID=@IR_ID");

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@IR_ID", DbType.String, IR_ID);

            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }

        }


    }
}
